#include <Wire.h>

uint8_t outbuf[6];

int hit = 0;

int ledPin = 13;

void
receiveEvent (int howMany)
{
  digitalWrite (ledPin, HIGH);	// sets the LED on

  while (Wire.available ())
    {
      char c = Wire.receive ();	// receive byte as a character 
    }
}
void
requestEvent ()
{
  digitalWrite (ledPin, HIGH);	// sets the LED on


  outbuf[0] = nunchuk_encode_byte (125);
  outbuf[1] = nunchuk_encode_byte (126);
  outbuf[2] = nunchuk_encode_byte (227);
  outbuf[3] = nunchuk_encode_byte (241);

  outbuf[4] = nunchuk_encode_byte (140);

  outbuf[5] = nunchuk_encode_byte (1);	// Press C button
  //outbuf[5] = nunchuk_encode_byte(2); // Press Z button
  //outbuf[5] = nunchuk_encode_byte(0); // Press Z and C button

  Wire.send (outbuf, 6);	// send data packet
}

void
setup ()
{
  pinMode (ledPin, OUTPUT);	// sets the digital pin as output

  Wire.begin (0x52);		// join i2c bus with address 0x52
  Wire.onReceive (receiveEvent);	// register event
  Wire.onRequest (requestEvent);	// register event
}

void
loop ()
{
  delay (100);
}


char
nunchuk_encode_byte (char x)
{
  x = x - 0x17;
  x = (x ^ 0x17);
  return x;
}
